<template>
  <div>
    <div class="mui-input-group">
      <div class="mui-input-row">
          <label>用户名</label>
          <input type="text" class="mui-input-clear" v-model="regForm.username" placeholder="请输入用户名">
      </div>
      <div class="mui-input-row">
          <label>密码</label>
          <input type="password" v-model="regForm.password" class="mui-input-password" placeholder="请输入密码">
      </div>
      <div class="mui-input-row">
          <label>确认密码</label>
          <input type="text" class="mui-input-password" v-model="regForm.rePassword" placeholder="请再次输入密码">
      </div>
      <div class="mui-input-row">
          <label>邮箱</label>
          <input type="text" class="mui-input-clear" v-model="regForm.email" placeholder="请输入邮箱">
      </div>
    </div>

    <button class="btn" @click="register">注 册</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      regForm: {
        username: '',
        password: '',
        rePassword: '',
        email: ''
      }
    }
  },
  created() {

  },
  methods: {
    // 注册
    register() {
      // 判断用户是否已经输入账号，密码，重复密码，邮箱
      if (this.regForm.username === '') {
          this.$toast('账号不能为空');
          return;
      }
      if (this.regForm.password === '') {
          this.$toast('密码不能为空');
          return;
      }
      if (this.regForm.rePassword === '') {
          this.$toast('确认密码不能为空');
          return;
      }
      if (this.regForm.email === '') {
          this.$toast('邮箱不能为空');
          return;
      }
      // 判断用户两次输入的密码一致
      if (this.regForm.password !== this.regForm.rePassword) {
          this.$toast('两次密码输入必须一致');
          return;
      }
      // 发送注册请求
      this.$http.post('register', this.regForm).then(res => {
        console.log(res);
        
        if (res.data.code === 1) {
          // 注册后立即登录
          this.$store.commit('user/setUser', res.data.data)
          this.$auth.setAuthorization(res.data.data.session_id)
          this.$toast(res.data.msg);
          this.$router.push('/user')
        } else {
          this.$toast(res.data.msg);
        }
      })
      
    }
  }

}
</script>

<style lang="scss" scoped>
.mui-input-group {
  margin-top: 10px;
  background-color: transparent;
  .mui-input-row {
    margin-top: 10px;
    background-color: #fff;
    label {
      width: 30%;
    }
    .mui-input-clear, .mui-input-password {
      width: 70%;
    }
  }
  
}
.btn {
    width: 90%;
    margin-left: 5%;
    margin-top: 20px;
    background-color: rgb(0, 122, 255);
    color: #fff;
    font-size: 16px;
  }
.register {
  display: block;
  margin-top: 20px;
  text-align: center;
  font-size: 14px;
}
</style>